package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.model.entity.ServiceBean;
import com.sgb.kjwl.model.entity.enterprise.SecerTitleEntity;
import com.sgb.kjwl.view.ui.adapter.enterprise.ChildLevelAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.ParentLevelAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.ServiceChildAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.ServiceFirstAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;

public class ServicePopWindow extends BasePopWindow {

    private MaxHeightRecyclerView recyclerView;
    private MaxHeightRecyclerView childRecyclerView;
    private TextView okView;
    private TextView resetView;
    private TextView lineView;
    private TextView tv_s;
    private LinearLayout bottomLayout;
    private ServiceFirstAdapter parentAreaAdapter;//一级适配器
    private ServiceChildAdapter childAreaAdapter;//二级适配器
    private List<ParentAreaEntity<List<ParentAreaEntity>>> parentList;
    private List<ParentAreaEntity> childList;
    private int parentPosition;
    private int parentPosition2;
    private int childPosition;
    private TwoAreaPopWindow.OnCloseBack onCloseBack;
    ServiceBean serviceBean = new ServiceBean();


    public ServicePopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_two_level2;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.rlv_list);
        childRecyclerView = holder.getPop().findViewById(R.id.rlv_child);
        bottomLayout = holder.getPop().findViewById(R.id.ll_next);
        okView = holder.getPop().findViewById(R.id.tv_ok);
        resetView = holder.getPop().findViewById(R.id.tv_next);
        lineView = holder.getPop().findViewById(R.id.line_view);
        tv_s = holder.getPop().findViewById(R.id.tv_s);
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultClick != null) {
                    onResultClick.result(false, serviceBean, null);
                }
                dissMiss();
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recyclerView.setVisibility(View.VISIBLE);
                childRecyclerView.setVisibility(View.GONE);
                bottomLayout.setVisibility(View.GONE);
                serviceBean.setChildName("");//二级名称
                serviceBean.setChildNo("");//二级id
            }
        });

        parentAreaAdapter = new ServiceFirstAdapter(mContext, null);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(parentAreaAdapter);
        parentAreaAdapter.setItemSelect(true);
        parentAreaAdapter.setDefItem(0);
        parentAreaAdapter.setOnItemClick(new AdapterOnItemClick<ParentAreaEntity<List<ParentAreaEntity>>>() {
            @Override
            public void onItemClick(ParentAreaEntity<List<ParentAreaEntity>> listParentAreaEntity, int position) {
                tv_s.setText(listParentAreaEntity.getName());
                serviceBean.setParentName(listParentAreaEntity.getName());//一级名称
                serviceBean.setParentNo(listParentAreaEntity.getId());//一级id
                parentAreaAdapter.setChoiceindex(position);
                if (listParentAreaEntity.getName().equals("不限")) {
                    parentPosition2 = parentPosition;
                    parentPosition = position;
                    if (onResultClick != null) {
                        onResultClick.result(true, serviceBean, null);
                    }
                    dismiss();
                } else {
                    parentPosition2 = parentPosition;
                    parentPosition = position;
                    showChildList(listParentAreaEntity);
                }
            }
        });

        childAreaAdapter = new ServiceChildAdapter(mContext, null);
        childRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        childRecyclerView.setAdapter(childAreaAdapter);
        childAreaAdapter.setItemSelect(true);
        childAreaAdapter.setDefItem(0);
        childAreaAdapter.setOnItemClick(new AdapterOnItemClick<ParentAreaEntity>() {
            @Override
            public void onItemClick(ParentAreaEntity childEntity, int position) {
                childAreaAdapter.setChoiceindex(position);
                serviceBean.setChildName(childEntity.getName());//二级名称
                serviceBean.setChildNo(childEntity.getId());//二级id
                childPosition = position;
            }
        });
    }

    public void setData(List<ParentAreaEntity<List<ParentAreaEntity>>> parentList) {
        this.parentList = parentList;
        parentAreaAdapter.setDatas(parentList);
        tv_s.setText(parentList.get(0).getName());
    }


    /**
     * @param parentList 12级筛选数据
     * @param entit      选中数据
     */
    public void setData(List<ParentAreaEntity<List<ParentAreaEntity>>> parentList, SecerTitleEntity entit) {
        this.parentList = parentList;

        parentAreaAdapter.setDatas(parentList);
        tv_s.setText(parentList.get(0).getName());

        if (!TextUtils.isEmpty(entit.getCategorySecondName()) &&entit.getCategorySecondId() > 0){
            tv_s.setText(entit.getCategorySecondName());
            for (int i = 0; i < parentList.size(); i++) {

                if (parentList.get(i).getName().equals(entit.getCategorySecondName()) && parentList.get(i).getId().equals(entit.getCategorySecondId() + "")) {
                    parentPosition2 = parentPosition;
                    parentPosition = i;
                    if(parentAreaAdapter!=null){
                        parentAreaAdapter.setChoiceindex(i);
                    }
                    serviceBean.setParentName(parentList.get(i).getName());
                    serviceBean.setParentNo(parentList.get(i).getId());
                    showChildList(parentList.get(i));
                    break;
                }

            }

        }

        if (!TextUtils.isEmpty(entit.getCategoryThirdName()) && entit.getCategoryThirdId() > 0) {
            tv_s.setText(entit.getCategoryThirdName());

            serviceBean.setChildName(entit.getCategoryThirdName());//二级名称
            serviceBean.setChildNo(entit.getCategoryThirdId()+"");//二级id

            for (int i = 0; i < parentList.get(parentPosition).getChildren().size(); i++) {
                if(entit.getCategoryThirdName().equals(parentList.get(parentPosition).getChildren().get(i).getName())){
                    childPosition = i;
                    if(childAreaAdapter!=null){
                        childAreaAdapter.setChoiceindex(childPosition);
                    }
                    break;
                }
            }
        }
    }


    public void showChildList(ParentAreaEntity<List<ParentAreaEntity>> listParentAreaEntity) {


        if(listParentAreaEntity.getChildren().size()==1&&listParentAreaEntity.getChildren().get(0).getName().equals("不限")){

            if (onResultClick != null) {
                onResultClick.result(false, serviceBean, null);
            }

            dismiss();
            return;
        }

        if (parentPosition == parentPosition2) {
            serviceBean.setChildName(listParentAreaEntity.getChildren().get(childPosition).getName());//二级名称
            serviceBean.setChildNo(listParentAreaEntity.getChildren().get(childPosition).getId());//二级id
        } else {
            childAreaAdapter.setChoiceindex(0);
            serviceBean.setChildName(listParentAreaEntity.getChildren().get(0).getName());//二级名称
            serviceBean.setChildNo(listParentAreaEntity.getChildren().get(0).getId());//二级id
        }
        childAreaAdapter.setDatas(listParentAreaEntity.getChildren());
        recyclerView.setVisibility(View.GONE);
        childRecyclerView.setVisibility(View.VISIBLE);
        bottomLayout.setVisibility(View.VISIBLE);
    }

    public void setTitleGone() {
        tv_s.setVisibility(View.GONE);
    }


    @Override
    public void dismiss() {
        super.dismiss();
        if (onDisMiss != null) {
            onDisMiss.disMiss();
        }
    }
}
